package com.example.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.DTO.ResultDTO;
import com.example.demo.entity.Menu;
import com.example.demo.entity.Role;
import com.example.demo.entity.User;
import com.example.demo.mapper.UserMapper;
import com.example.demo.service.RoleMenuService;
import com.example.demo.service.UserRoleService;
import com.example.demo.service.UserService;
import com.example.demo.util.TokenUtils;
import com.example.demo.vo.RegisterRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author moguai
 * @version 1.0
 * @date 2022/3/18 9:25
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Autowired
    private TokenUtils tokenUtils;
    @Autowired
    private UserRoleService userRoleService;
    @Autowired
    private RoleMenuService roleMenuService;

    @Override
    public ResultDTO register(RegisterRequest registerRequest) {
        int count = (int) this.count(new QueryWrapper<User>()
                .eq("email", registerRequest.getEmail()));
        if (count > 0) {
            return ResultDTO.errorOf("邮箱被占用");
        }
        ;
        User temp = new User();
        temp.setName(registerRequest.getName());
        temp.setCreate_time(System.currentTimeMillis());
        temp.setPassword(registerRequest.getPassword());
        temp.setEmail(registerRequest.getEmail());
        this.save(temp);
        String token = tokenUtils.getToken(temp);
        User Info = this.getOne(new QueryWrapper<User>().eq("email", registerRequest.getEmail()));
        TokenUtils.setTokenWhite(token, Info.getId());
        return ResultDTO.okOf(token);
    }

    @Override
    public ResultDTO login(String mail, String password) {
        Map<String, Object> map = new HashMap<>();
        User user = this.getOne(new QueryWrapper<User>().eq("email", mail));
        if (user == null) {
            return ResultDTO.errorOf("用户不存在");
        }
        if (!user.getPassword().equals(password)) {
            return ResultDTO.errorOf("密码错误");
        }
        String token = tokenUtils.getToken(user);
        TokenUtils.setTokenWhite(token, user.getId());
        List<Role> roles = userRoleService.selectRoleByUser(user.getId());
        List<Menu> menus = roleMenuService.selectMenuByUser(user.getId());
        map.put("userId", user.getId());
        map.put("userName", user.getName());
        map.put("token", token);
        map.put("role", roles);
        map.put("menu", menus);
        return ResultDTO.okOf(map);
    }

    @Override
    public void logout(String token) {

        if (TokenUtils.isTokenWhite(token)) {
            TokenUtils.deleteTokenWhite(token);
            TokenUtils.setTokenBlack(token);
        }
    }
}
